package cn.iotab.iotkit.transport.mqtt.handler;

import cn.iotab.iotkit.transport.mqtt.MqttTransportContext;
import cn.iotab.iotkit.transport.mqtt.session.MqttSessionCtx;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;

public class PingHandler extends AbstractMqttMessageHandler<MqttMessage>{
    public PingHandler(MqttTransportContext context, MqttSessionCtx deviceSessionCtx) {
        super(context, deviceSessionCtx);
    }

    @Override
    public void process(ChannelHandlerContext ctx, MqttMessage msg){
        if (checkConnected(ctx, msg)) {
            ctx.writeAndFlush(new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0)));
            transportDriverService.sessionActivity(deviceSessionCtx.getSessionInfo());
        }
    }
}
